package FirstChapter.five;

import java.util.Scanner;

/**
 * Created by Lee on 2017/2/18.
 */
public class UnionFind {
    private int[] id;//Id of component
    private int count;//count of component

    /*
    * init Compoint  Array*/
    public UnionFind(int N) {
        count = N;
        id = new int[N];
        for (int i = 0; i < N; i++) {
            id[i] = i;
        }
    }

    public int count (){
        return count;
    }

    public boolean isConnected(int p, int q) {
        return find(p) == find(q);
    }

    public int find(int p) {
        return id[p];
    }

    public void union(int p, int q) {
        int pID = find(p);
        int qID = find(q);

        if (pID == qID) {
            return;
        }

        for (int i=0;i<id.length;i++) {
            if (id[i] == pID) {
                id[i] = qID;
            }
        }
        count--;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        UnionFind uf = new UnionFind(N);
        while (true) {
            int p = Integer.parseInt(sc.next());
            int q = Integer.parseInt(sc.next());
            if (p == -1 || q == -1) {
                break;
            }
            uf.union(p, q);
        }
        System.out.println(uf.count() + "components");
    }

}
